package unpackaged;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

/**
 *@ClassName:ReceiveClient
 *@author: pengyu  
 *@date :2012-8-3 下午2:17:26
 *@Description:TODO 
 */
public class ReceiveClient {
    private final String IP="127.0.0.1";
    private final int PORT=45678;
//    private final String IP="192.168.5.208";
//    private final int PORT=8090;
	//发送
    /**
     * @throws Exception 
     * 发送报文
     * @Title: send 
     * @Description: TODO 
     * @param @param reqMessage   
     * @return void   
     * @throws
     */
   public void send(String reqMessage) throws Exception{
       Socket sock=null;
       BufferedOutputStream out=null;
       try {
        sock=new Socket();

                  SocketAddress sockAdd=new InetSocketAddress(IP, PORT);
             sock.connect(sockAdd, 2000); //客户端设置连接建立超时时间
             out=new BufferedOutputStream(sock.getOutputStream());
        out.write(reqMessage.getBytes("utf-8"));
        out.flush();

    } catch (UnknownHostException e) {
        System.out.println("网络连接异常"+e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
    	  System.out.println("网络连接异常"+e.getMessage());
        e.printStackTrace();
    }finally{
        if(out!=null){
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();            }
        }
        if(sock!=null){
            try {
                sock.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
        }
        }
    } 
   }

    //接收
    public String  reiceve() throws Exception{
        Socket sock=null;
        BufferedInputStream in=null;

            try {
                sock=new Socket(IP,PORT);
                in = new BufferedInputStream(sock.getInputStream());
                 if ((sock == null) || (in == null)) {
                        throw new Exception("套接口无效，无法读取数据");
                  }

            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             byte[] bts = new byte[10000];
             int totalLen = 0, len = 0;
             while ((len = in.read(bts, totalLen, 1000)) != -1) {
                    totalLen += len;
                }
             String result = new String(bts);  //注意字符编码
             return result.trim();
    } 

//main函数示例

    public static void main(String[] args) throws UnsupportedEncodingException{
        //发送报文

        //发送
//              String str="我是客户端！";   
    	//case "A1"://6.1.1 注册签到    1.1  
      String str="0313{\"fd\":\"222\",\"method\":\"chargerLogin\",\"protocol_type\":\"1\",\"protocol_version\":\"0101\",\"chargerId\":\"0028300000000025\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C000100000000010028300000000025A12015112610101400001234000056780102201505190000000800000000000000000300\"}";   
              
//              String str1 =  "{\"method\":\"chargerLogin\",\"protocol_type\":\"1\",\"protocol_version\":\"0101\",\"chargerId\":\"0028300000000025\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C000100000000010028300000000025A12015112610101400001234000056780102201505190000000800000000000000000300\"}";
//              System.out.println("len:" + str1.getBytes("utf-8").  );
        //6.1.2	签退
//    	String str="0247{\"method\":\"chargerLogout\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.35\",\"source_port\":\"11517\",\"data\":\"6800000100000000010028300000000025A320160127153032\"}";   
        //6.4.1  刷卡请求：requestPayByCard
//    	String str="0269{\"method\":\"requestPayByCard\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.35\",\"source_port\":\"11517\",\"data\":\"680000010000000001002830000000002575201601271530320086135535231478095531\"}";   
//    	String str="0272{\"method\":\"requestPayByCard\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.35\",\"source_port\":\"11517\",\"data\":\"680000010000000001002830000000002575201601271530320086135535231478095531\"}";   
//    	6.5.1心跳报文
    	//0101
//    	String str="0245{\"method\":\"uploadHeart\",\"protocol_type\":\"1\",\"protocol_version\":\"0101\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C0001000000000100283000000000255120151126101014\"}";
    	//0103
//    	String str="0245{\"method\":\"uploadHeart\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C0001000000000100283000000000255120151126101014\"}";
//    	6.5.2主动遥变上传
    	//0101 0250
//    	String str="0250{\"method\":\"uploadChange\",\"protocol_type\":\"1\",\"protocol_version\":\"0101\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C00010000000001002830000000002553201511261010140101\"}";
    	//0103 0256
//    	String str="0256{\"method\":\"uploadChange\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C000100000000010028300000000025532015112610101401FFFF0200\"}";
    	//6.5.3固件更新状态上传
//    	String str="0264{\"method\":\"uploadFirmwareStatus\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C0001000000000100283000000000255A2015112610101401FFFFFFFF\"}";
    	//6.5.3固件更新状态上传
//    	String str="0264{\"method\":\"uploadFirmwareStatus\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C0001000000000100283000000000255A2015112610101401FFFFFFFF\"}";
    	//6.5.4账单主动上传
    	//0101
//    	String str="0264{\"method\":\"uploadBill\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"681C0001000000000100283000000000255A2015112610101401FFFFFFFF\"}";
    	//0103
//    	String str="0264{\"method\":\"uploadBill\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"684B00010000000001002830000000002554201511261021222015112610190520151126102121012345678901234567890000004D0000004D000000000000000000000000000000000000010X00010000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\"}";
    	//6.5.5充电数据上传
//    	String str="0326{\"method\":\"uploadChargeData\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"68260001000000000100283000000000255C2015112610212220151126102122000000001200000012100E00001D5700800C0010000000BC1B00FFFFFF0101\"}";
    	//6.5.6电池详情报文（可选）
//    	String str="0341{\"method\":\"uploadBatteryData\",\"protocol_type\":\"1\",\"protocol_version\":\"0103\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"682D0001000000000100283000000000255D2015112610212200010001000100011111000101010101010101FFFFFFFFFFFF0101000101010101010101010101010101010101\"}";
    	//遥测数据上传：summonReturnUploadTelemetry 72
//    	String str="0291{\"method\":\"summonReturnUploadTelemetry\",\"protocol_type\":\"1\",\"protocol_version\":\"0101\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"682D00010000000001002830000000002572201512131234561D5700800C0010000010000000BC1B\"}";

    	//遥信数据上传:summonReturnUploadTelesignal 73 
//    	String str="0270{\"method\":\"summonReturnUploadTelesignal\",\"protocol_type\":\"1\",\"protocol_version\":\"0101\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"682D000100000000010028300000000025732015121312345601000001\"}";

    	//遥脉数据上传：summonReturnUploadPulse 74 
//    	String str="0273{\"method\":\"summonReturnUploadPulse\",\"protocol_type\":\"1\",\"protocol_version\":\"0101\",\"chargerId\":\"0028300000000010\",\"ip\":\"192.168.5.208\",\"port\":\"8090\",\"source_ip\":\"192.168.5.12\",\"source_port\":\"24569\",\"data\":\"682D00010000000001002830000000002574201512131234561000000010000000\"}";

    	/*
          * 注册签到：chargerLogin
			登出：chargerLogout
          	刷卡请求：requestPayByCard
			心跳数据上传：uploadHeart
			遥变：uploadChange
			固件更新上传：uploadFirmwareStatus
			账单主动上传：uploadBill
			充电数据上传：uploadChargeData
			电池详情报文上传：uploadBatteryData
			遥测数据上传：summonReturnUploadTelemetry
			遥信数据上传:summonReturnUploadTelesignal
			遥脉数据上传：summonReturnUploadPulse
          * */      
        try {
                new ReceiveClient().send(str);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        //接收报文
        /*try {
            String recStr=new Receiver().reiceve();
            System.out.println("客户端接收到的结果=="+recStr);
                    } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }
}
